//
//  CrazyChatOrderListView.m
//  webSocket
//
//  Created by 马军凯 on 2019/2/20.
//  Copyright © 2019年 ZH. All rights reserved.
//

#import "CrazyChatOrderListView.h"
#import "CrazyChatOrderListCell.h" //cell

@interface CrazyChatOrderListView () <UITableViewDataSource, UITableViewDelegate>
{
    int _page;
    int _allPage;
}

@property (nonatomic, strong) UITableView *orderListTableView;
@property (nonatomic, retain) NSMutableArray *orderListDataArray; //数据源

@end

@implementation CrazyChatOrderListView

- (void)dealloc {
    NSLog(@"订单列表页销毁");
}

//单例
+ (instancetype)sharedDefault {
    return [[self alloc] init];
}

//初始化
- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [CRAZY_RGBR(114) colorWithAlphaComponent:0.5];
        //加载子视图
        [self setLayoutSubView];
    }
    return self;
}

//加载子视图
- (void)setLayoutSubView {
    //白色背景
    UIView *whiteBackView = [[UIView alloc] init];
    whiteBackView.frame = CGRectMake(0, CRAZY_SCREEN_HEIGHT / 3, CRAZY_SCREEN_WIDTH, CRAZY_SCREEN_HEIGHT * 2 / 3);
    whiteBackView.backgroundColor = [UIColor whiteColor];
    [self addSubview:whiteBackView];
    //关闭
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.frame = CGRectMake(whiteBackView.width - 40, 0, 40, 40);
    [closeBtn setImage:[UIImage imageNamed:@"CrazyChat_close@2x.png"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(removeSelf) forControlEvents:UIControlEventTouchUpInside];
    [whiteBackView addSubview:closeBtn];
    //描述
    UILabel *descripLabel = [[UILabel alloc] init];
    descripLabel.frame = CGRectMake(10, 10, whiteBackView.width - 60, 20);
    descripLabel.text = @"选择咨询订单";
    descripLabel.textColor = CRAZY_RGB(142, 148, 155);
    descripLabel.font = [UIFont systemFontOfSize:14];
    [whiteBackView addSubview:descripLabel];
    //线
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(0, 40, whiteBackView.width, 1);
    lineView.backgroundColor = CRAZY_RGBR(242);
    [whiteBackView addSubview:lineView];
    //tableView
    self.orderListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 41, whiteBackView.width, whiteBackView.height - 41) style:UITableViewStyleGrouped];
    self.orderListTableView.delegate = self;
    self.orderListTableView.dataSource = self;
    self.orderListTableView.backgroundColor = [UIColor whiteColor];
    [self.orderListTableView registerClass:[CrazyChatOrderListCell class] forCellReuseIdentifier:@"CrazyChatOrderListCell"];
    [whiteBackView addSubview:self.orderListTableView];
    self.orderListTableView.tableFooterView = [UIView new];
}

#pragma mark UITableView dataSource
//几组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.orderListDataArray.count;
}
//每组个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CrazyChatOrderListDataModel *dataModel = self.orderListDataArray[section];
    return dataModel.order_goods.count;
}
#pragma mark UITableView delegate
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CrazyChatOrderListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CrazyChatOrderListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CrazyChatOrderListCell"];
    }
    
    CrazyChatOrderListDataModel *dataModel = self.orderListDataArray[indexPath.section];
    CrazyChatOrderListGoodsModel *goodsModel = dataModel.order_goods[indexPath.row];
    if (goodsModel) {
        cell.model = goodsModel;
    }
    
    return cell;
}
//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}
#pragma mark separator 分割线
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        cell.preservesSuperviewLayoutMargins = NO;
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        cell.layoutMargins = UIEdgeInsetsZero;
    }
}
#pragma mark UITableView headerView
//view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CrazyChatOrderListDataModel *dataModel = self.orderListDataArray[section];
    UIView *headView = [[UIView alloc] init];
    headView.frame = CGRectMake(0, 0, CRAZY_SCREEN_WIDTH, 40);
    headView.backgroundColor = [UIColor whiteColor];
    //店铺名称
    UILabel *shopNameLabel = [[UILabel alloc] init];
    shopNameLabel.frame = CGRectMake(10, 10, headView.width / 2 - 10, 20);
    shopNameLabel.textColor = CRAZY_RGBR(33);
    shopNameLabel.text = dataModel.store.store_name;
    shopNameLabel.font = [UIFont systemFontOfSize:14];
    [headView addSubview:shopNameLabel];
    //状态
    UILabel *statusLabel = [[UILabel alloc] init];
    statusLabel.frame = CGRectMake(headView.width / 2, 10, headView.width / 2 - 10, 20);
    statusLabel.textColor = CRAZY_RGB(244, 0, 0);
    statusLabel.text = @"卖家已发货";
    statusLabel.font = [UIFont systemFontOfSize:14];
    statusLabel.textAlignment = NSTextAlignmentRight;
    [headView addSubview:statusLabel];
    
    switch ([dataModel.status integerValue]) {
        case 0: {
            statusLabel.text = @"待付款";
        }
            break;
        case 1: {
            statusLabel.text = @"待配送";
        }
            break;
        case 2: {
            statusLabel.text = @"配送中";
        }
            break;
        case 3: {
            statusLabel.text = @"已完成";
        }
            break;
        case 4: {
            statusLabel.text = @"已关闭";
        }
            break;
        case 5: {
            statusLabel.text = @"退款中";
        }
            break;
        case 6: {
            statusLabel.text = @"已取消";
        }
            break;
        default:
            break;
    }
    
    
    return headView;
}
//高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
#pragma mark UITableView footerView
//view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
//高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0000000001;
}
#pragma mark UITableView didselect
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CrazyChatOrderListDataModel *dataModel = self.orderListDataArray[indexPath.section];
    CrazyChatOrderListGoodsModel *goodsModel = dataModel.order_goods[indexPath.row];
    if (self.block) {
        self.block(dataModel.order_attach_id, goodsModel.goods_id);
    }
    [self removeFromSuperview];
}


#pragma mark 赋值店铺ID 请求网络接口
- (void)setStore_id:(NSString *)store_id {
    _store_id = store_id;
    //刷新
    [self createRefresh];
    //网络请求
    [self headRefresh];
}

#pragma mark 刷新
- (void)createRefresh {
    MJWeakSelf;
    self.orderListTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf headRefresh];
    }];
    self.orderListTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf footRefresh];
    }];
    self.orderListTableView.mj_footer.ignoredScrollViewContentInsetBottom = CRAZY_IS_iPhoneX_up ? CRAZY_HOME_HEIGHT : 0;
}
//刷新
- (void)headRefresh {
    _page = 1;
    [self.orderListDataArray removeAllObjects];
    [self.orderListTableView reloadData];
    [self getOrderListData];
}
//加载更多
- (void)footRefresh {
    if (_page > _allPage) {
        [self.orderListTableView.mj_footer endRefreshingWithNoMoreData];
        [self.orderListTableView.mj_footer endRefreshing];
    } else {
        [self getOrderListData];
    }
}
//结束刷新
- (void)endRefresh {
    [self.orderListTableView.mj_header endRefreshing];
    [self.orderListTableView.mj_footer endRefreshing];
}

#pragma mark 网络请求
- (void)getOrderListData {
    NSString *urlString = [NSString stringWithFormat:@"%@%@", CRAZY_CHAT_API_DOMIN, @"customer/getStoreOrderList"];
    NSDictionary *params = @{@"member_id" : UID, @"store_id" : self.store_id, @"page" : [NSString stringWithFormat:@"%d", _page]};
    CrazyWeakSelf;
    [CrazyNetWork CrazyRequest_Post:urlString HUD:YES parameters:params success:^(NSDictionary *dic, NSString *url, NSString *Json) {
        if ([[NSString stringWithFormat:@"%@",dic[@"code"]] isEqualToString:@"0"]) {
            [weakSelf receiveDataWith:dic[@"data"]];
        } else {
            [JKToast showWithText:dic[@"data"]];
        }
        [weakSelf endRefresh];
    } fail:^(NSError *error, NSString *url) {
        [weakSelf endRefresh];
    }];
}
//保存数据
- (void)receiveDataWith:(NSDictionary *)dic {
    CrazyChatOrderListModel *allModel = [[CrazyChatOrderListModel alloc] init];
    allModel = [CrazyChatOrderListModel objectWithKeyValues:dic];
    
    int total = [allModel.total intValue];
    int per_page = [allModel.per_page intValue];
    if (0 == total % per_page) {
        _allPage = total / per_page;
    } else {
        _allPage = total / per_page + 1;
    }
    _page = _page + 1;
    
    for (CrazyChatOrderListDataModel *model in allModel.data) {
        [self.orderListDataArray addObject:model];
    }
    [self.orderListTableView reloadData];
}

#pragma mark 显示
- (void)showOrderListView {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.bounds;
    [window addSubview:self];
}

//消失
- (void)removeSelf {
    [self removeFromSuperview];
}

#pragma mark 懒加载
- (NSMutableArray *)orderListDataArray {
    if (_orderListDataArray == nil) {
        _orderListDataArray = [NSMutableArray array];
    }
    return _orderListDataArray;
}

@end
